import java.util.LinkedList;
import java.util.List;

/**
 * 257. 二叉树的所有路径
 * https://leetcode.cn/problems/binary-tree-paths/description/
 */
class Solution2 {
    List<String> ret;
    public List<String> binaryTreePaths(TreeNode root) {
        ret = new LinkedList<>();
        dfs(root, new StringBuilder());
        return ret;
    }

    public void dfs(TreeNode root, StringBuilder _path) {
        if(root == null) return;

        StringBuilder path = new StringBuilder(_path);
        path.append(String.valueOf(root.val));
        if(root.left != null || root.right != null) path.append("->");

        dfs(root.left, path);
        dfs(root.right, path);

        if(root.left == null && root.right == null) {
            String tmp = path.toString();
            ret.add(tmp);
            return;
        }
    }
}